package com.example.cardgame;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvName, tvGender, tvStudentId, tvPhone, tvEmail, tvHobby, tvHighScore;
    private Button btnEditInfo, btnCardGame;
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 设置应用标题
        setTitle(getString(R.string.app_name));

        initViews();
        initData();
        setupClickListeners();
    }

    private void initViews() {
        tvName = findViewById(R.id.tv_name);
        tvGender = findViewById(R.id.tv_gender);
        tvStudentId = findViewById(R.id.tv_student_id);
        tvPhone = findViewById(R.id.tv_phone);
        tvEmail = findViewById(R.id.tv_email);
        tvHobby = findViewById(R.id.tv_hobby);
        tvHighScore = findViewById(R.id.tv_high_score);
        btnEditInfo = findViewById(R.id.btn_edit_info);
        btnCardGame = findViewById(R.id.btn_card_game);
    }

    private void initData() {
        userInfo = new UserInfo();
        updateUI();
    }

    private void updateUI() {
        tvName.setText(userInfo.getName());
        tvGender.setText(userInfo.getGender());
        tvStudentId.setText(userInfo.getStudentId());
        tvPhone.setText(userInfo.getPhone());
        tvEmail.setText(userInfo.getEmail());
        tvHobby.setText(userInfo.getHobbies());
        tvHighScore.setText(String.valueOf(userInfo.getHighScore()));
    }

    private void setupClickListeners() {
        btnEditInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, EditInfoActivity.class);
                intent.putExtra("userInfo", userInfo);
                startActivityForResult(intent, 1);
            }
        });

        btnCardGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CardGameActivity.class);
                intent.putExtra("highScore", userInfo.getHighScore());
                startActivityForResult(intent, 2);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            userInfo = (UserInfo) data.getSerializableExtra("userInfo");
            updateUI();
        }

        if (requestCode == 2 && resultCode == RESULT_OK && data != null) {
            int newScore = data.getIntExtra("newScore", 0);
            if (newScore > userInfo.getHighScore()) {
                userInfo.setHighScore(newScore);
                tvHighScore.setText(String.valueOf(newScore));
            }
        }
    }
}